package com.smartscreen.homeschoolconnection.controller;

import com.smartscreen.common.response.ApiResponse;
import com.smartscreen.homeschoolconnection.dto.*;
import com.smartscreen.homeschoolconnection.service.HomeSchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class HomeSchoolController {

    @Autowired
    private HomeSchoolService homeSchoolService;

    /**
     * 1.1 绑定学生
     */
    @PostMapping("/me/bindings")
    public ApiResponse<StudentBindingInfo> bindStudent(
            @RequestHeader("X-User-Id") String parentId,
            @RequestBody BindStudentRequest request) {
        try {
            StudentBindingInfo result = homeSchoolService.bindStudent(parentId, request);
            return ApiResponse.success("绑定成功", result);
        } catch (Exception e) {
            return new ApiResponse<>(400, "学号或身份证信息不匹配，请核对后重试。", null);
        }
    }

    /**
     * 1.2 获取已绑定学生列表
     */
    @GetMapping("/me/bindings")
    public ApiResponse<List<StudentBindingInfo>> getBindings(
            @RequestHeader(name = "X-User-Id",required = false) String parentId) {
        List<StudentBindingInfo> bindings = homeSchoolService.getBindings(parentId);
        return ApiResponse.success(bindings);
    }

    /**
     * 2.1 获取我的消息列表
     */
    @GetMapping("/me/messages")
    public ApiResponse<Map<String, Object>> getMessages(
            @RequestHeader(name = "X-User-Id",required = false) String parentId,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String type) {
        Map<String, Object> result = homeSchoolService.getMessages(parentId, page, pageSize, type);
        return ApiResponse.success(result);
    }

    /**
     * 2.2 将消息标记为已读
     */
    @PostMapping("/me/messages/{messageId}/read")
    public ApiResponse<Void> markMessageAsRead(@PathVariable String messageId) {
        homeSchoolService.markMessageAsRead(messageId);
        return ApiResponse.success("操作成功", null);
    }

    /**
     * 3.1 发送消息给教师
     */
    @PostMapping("/conversations")
    public ApiResponse<Map<String, String>> sendMessageToTeacher(
            @RequestHeader(name = "X-User-Id",required = false) String parentId,
            @RequestBody SendMessageRequest request) {
        Map<String, String> result = homeSchoolService.sendMessageToTeacher(parentId, request);
        return ApiResponse.created("消息发送成功", result);
    }

    /**
     * 3.2 获取对话列表
     */
    @GetMapping("/conversations")
    public ApiResponse<List<ConversationInfo>> getConversations(
            @RequestHeader(name = "X-User-Id",required = false) String userId,
            @RequestHeader(name = "X-User-Type",required = false) String userType) {
        List<ConversationInfo> conversations = homeSchoolService.getConversations(userId, userType);
        return ApiResponse.success(conversations);
    }

    /**
     * 3.3 获取指定对话的消息记录
     */
    @GetMapping("/conversations/{conversationId}/messages")
    public ApiResponse<List<MessageInfo>> getConversationMessages(
            @PathVariable String conversationId) {
        List<MessageInfo> messages = homeSchoolService.getConversationMessages(conversationId);
        return ApiResponse.success(messages);
    }
}